package cn.com.yang.net;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import cn.com.yang.utils.DateUtil;
import cn.com.yang.utils.RSAUtil;

/**
 * 礼品请求报文测试
 * TODO
 * @author yangyijin
 * @date 2023年2月11日-下午11:43:59
 */
public class GiftRestFulApi {
	//德众
	private String DZ_RSA_PUBKEY_DEV ="MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAjV8cG7dVIatUHXY53RJS+hL7LgGtHCUnJjpF6saoawRciauQkJZMDK8vTt0aj05pWoxZsKJEiqynP4CFxoQXRm8UZmNZbZs47n15bSAHfjJYJBfzXIwaC7RFDswSkDbKHLp0rlCT66Q82PmcoStfZHFP6dXUQ8PCcN/QPHkzSVBOjPQiAKIzrt9AY/noUIlrosiVsJ8GVCSBp9I9D/mbqYkfus8mfwsUCcU6PCIBdDxOkJekLKHGs5Fx70eGnCJuhcqXhUG/9xBoyBJnKfKXUCUOhP4FgCf3kaMD45VXH9/I/Wd8IZc2rO4FV0YAPtgMMcefz/CrZ+CfzNhWs47w9pL6jLIDKQxtAtTbfMv3kDeTmj2bQ3S0ETB5CtBSFV7XvgU4J3c6GHo614RYTlJeNLW056hKNGxGCWyfL68wN9deStPJAO1CWAdZFaHHVFvVV96bnkwVu5AS/zh18B8DA4uwyn0gKiM94fCdYTZ3Hn4zr/obw8UQKQlFg/tyLR7ENji5aeWBVi3FGjvDr4RF7jXUAlzl5J+SVsWegMU0YZuk5qZa4JI3HVtNGkfqYLDv/lbLb37qJ+71/qO3olPOB8g56Xqfz/SXaG7FyLUZeMZ20fLQ5hN0sA1onUOLPRyPN2Du6yc6uNJ3/9D+1xewE8DWhHcZZGssNr0KFmvSxgECAwEAAQ==";
	private String DZ_ORDER_DEV="http://hbyhtest.duobashuzi.com/api/HbOrder/order";

	private String DZ_SIGN_DEV = "http://hbyhtest.duobashuzi.com/api/HbOrder/signFor";
	//平南
	private String PN_RSA_PUBKEY_DEV ="MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAoZCUcbMVJ/VC42u9+W7EjWaH2gF/vseZMvNhRoj0vB0Ete9FdkRG50M9KQOnC3Derd7riGLPeAPZmvHFWOGIGH2j0Ks8VvxDZ9m1G1pOASxsLvcwdktD4plbFH1ak9WHhffIwvjXi6MRUseIRj3VSTSujrwLJ8eilB5GZFyviaw27YY4l2mGAPGqZuLs5qkeeqYO0KJLdsoxf1Vje0ci3PTZkX4pyWGHbVbGSRZ9H2NmEX92kiisWWk5jdK17Gdwn0A8ZvCJuoVidvoFkp1Q5NKRt6Sh225YLy9ZGLug/wEy/UayI45PtkuJm2xlnx/nI7viCDTrO7EUTsEnxmbLI2/EdYp0y07zg8pA1dvhe5DWgSr08zcNT4UIDoO7zbD3D0XkN7VwDZiTtWeYHWvv1q4Ja47MCZGw4dKTazuM4JUc6X6MESJ4fY1frELxu+kSiqm9+xyr9+P4XcRJJ3ESqZoMzDipmx93R8ajPU8jr+T2tQ3DCCq33dpq9ZHjQgUog8tGUhPmrGRSTyJladstSoD7wZRy/SxPDluIeDqot6ZOhm2rBd+q+1t9LYNNL30vK1eCbOR9i1i+VIrG2m+GvySGLrRaczIn9WYnoH6Zh4iw1Ja+U+xiXZzAeMKunp0ZSecaQMeLFMsC0XH1QHzIRTWzn/SOQ/QNBMnul37Zf0sCAwEAAQ==";
	private String PN_ORDER_DEV="http://domo.huicaib2b.com/api/order.php";
	private String PN_SIGN_DEV = "http://domo.huicaib2b.com/api/finish.php";

	/**
	 * 德众下单
	 * [{OrgId=0133, AddrDtl=彭后街道平安大街
, Phone=15032865500, TotalPrice=1174.7, OrgName=石家庄分行平安支行, Buyer=江莹, RcvAddr=[Ljava.lang.String;@7e7bd1f2, OrderIn
fo=[{Price=16.5, ProdName=福临门东北优质大米2.5kg, Num=50, ProdId=P2022030900007224}, {Price=29.7, ProdName=金龙鱼东北黄小米2
.5kg, Num=1, ProdId=P2022030900007229}, {Price=92, ProdName=福临门压榨一级花生油5L, Num=3, ProdId=P2022030900007233}, {Price=
44, ProdName=德世朗 兰斯弧形汤锅24cm, Num=1, ProdId=P2022030900007235}], OrderId=D20220406185302064330000000002}]

	 */
	@Test
	public void testOrder_DEZHONG() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "DZ-mfX3QAbIHW2k840P";
		String mchtId="DZ-FHUKECEkSssWJmHW";
		String orderId ="D20220412151233142340000000004";
		String TranSign = DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId);
		headers.set("TranSign",TranSign);
		System.out.println("签名信息："+TranSign);
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrgId","0126");
		params.put("OrgName","石家庄分行营业部");
		params.put("Buyer","江莹");
		params.put("Phone","13126164297");
		params.put("RcvAddr",new String[] {"河北省","石家庄","桥西区"});
		params.put("AddrDtl","平安大街79号");
		
		List<Map> list = new ArrayList();
		// P2022030900007236  德世朗朗悦多用两层蒸锅28cm   95.00

		Map<String, Object> prod1 = new HashMap<String, Object>();
		prod1.put("ProdId","P2022030900007236");
		prod1.put("ProdName","德世朗朗悦多用两层蒸锅28cm");
		prod1.put("Price",95.00);
		prod1.put("Num",10);
		list.add(prod1);
		// P2022030900007239  红豆家纺床单 49.5 
		Map<String, Object> prod2 = new HashMap<String, Object>();
		prod2.put("ProdId","P2022030900007239");
		prod2.put("ProdName","红豆家纺床单");
		prod2.put("Price",49.5);
		prod2.put("Num",2);
		list.add(prod2);
		params.put("TotalPrice",95+99);
		/*
		 * Map<String, Object> prod3 = new HashMap<String, Object>();
		 * prod2.put("ProdId","P2022030900007235");
		 * prod2.put("ProdName","德世朗 兰斯弧形汤锅24cm"); prod2.put("Price",198.00);
		 * prod2.put("Num",1); list.add(prod3);
		 */
		 
		
		
		/*
		 * Map<String, Object> prod4 = new HashMap<String, Object>();
		 * prod2.put("ProdId","P2022030900007229");
		 * prod2.put("ProdName","金龙鱼东北黄小米2.5kg"); prod2.put("Price",198.00);
		 * prod2.put("Num",1); list.add(prod4);
		 */
		 
		
		System.out.println("list.size():>>"+list.size());
		params.put("OrderInfo",list);
		
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		//测试RSA
		
		try {
			rsaStr = RSAUtil.encrypt(rsa,DZ_RSA_PUBKEY_DEV,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
	    System.out.println("datastr::>>"+dataStr);
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			//
			ResponseEntity<String> resultEntity = restTemplate.exchange(DZ_ORDER_DEV, HttpMethod.POST,entity,String.class);
			System.out.println("德众下单响应："+resultEntity.toString());
			System.out.println("德众下单响应body："+resultEntity.getBody().toString());
			System.out.println("德众下单响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 德众签收
	 */
	@Test
	public void testSign_DEZHONG() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "DZ-mfX3QAbIHW2k840P";
		String mchtId="DZ-FHUKECEkSssWJmHW";
		String orderId ="D20220412151233142340000000004";
		String TranSign = DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId);
		headers.set("TranSign",TranSign);
		
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrderNum","20220412150504871427");
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		try {
			rsaStr = RSAUtil.encrypt(rsa,DZ_RSA_PUBKEY_DEV,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange(DZ_SIGN_DEV, HttpMethod.POST,entity,String.class);
			System.out.println("德众签收响应："+resultEntity.toString());
			System.out.println("德众签收响应body："+resultEntity.getBody().toString());
			System.out.println("德众签收响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * // 授权码：9xAvjBbfDORd6tY8
		// 商户号：001117560760
	 * 平南下单
	 */
	@Test
	public void testOrder_PINGNAN() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "9xAvjBbfDORd6tY8";
		String mchtId="001117560760";
		String orderId ="D20220412152033142340000000001";
		headers.set("TranSign",DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId));
		
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrgId","0133");
		params.put("OrgName","平安支行");
		params.put("Buyer","苏杨");
		params.put("Phone","13126164297");
		params.put("RcvAddr",new String[] {"河北省","石家庄","桥西区"});
		params.put("AddrDtl","平安大街79号");
		// P2022030900007217  杉杉唯爱纯棉四件套 120
		List<Map> list = new ArrayList();
		Map<String, Object> prod1 = new HashMap<String, Object>();
		prod1.put("ProdId","P2022030900007217");
		prod1.put("ProdName","杉杉唯爱纯棉四件套");
		prod1.put("Price",120);
		prod1.put("Num",10);
		list.add(prod1);
		// P2022030900007209  福临门家香味传承土榨压榨一级花生油 96
		Map<String, Object> prod2 = new HashMap<String, Object>();
		prod2.put("ProdId","P2022030900007209");
		prod2.put("ProdName","福临门家香味传承土榨压榨一级花生油");
		prod2.put("Price",96);
		prod2.put("Num",10);
		list.add(prod2);
		params.put("OrderInfo",list);
		params.put("TotalPrice",1200 + 960);
		
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		try {
			rsaStr = RSAUtil.encrypt(rsa,DZ_ORDER_DEV,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
	
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange(PN_ORDER_DEV, HttpMethod.POST,entity,String.class);
			System.out.println("平南下单响应："+resultEntity.toString());
			System.out.println("平南下单响应body："+resultEntity.getBody().toString());
			System.out.println("平南下单响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 平南签收
	 */
	@Test
	public void testSign_PINGNAN() {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("Accept","application/json");
		headers.set("TranChan","HBMINI");
		headers.set("TranTime",DateUtil.parseDate(new Date(),"yyyyMMddHHmmss"));
		String authCode = "9xAvjBbfDORd6tY8";
		String mchtId="001117560760";
		String orderId ="D20220412152033142340000000001";
		String TranSign = DigestUtils.sha1Hex(authCode+":"+mchtId+":"+orderId);
		headers.set("TranSign",TranSign);
		
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("OrderId",orderId);
		params.put("OrderNum","SH20220412181114724732");
		String rsa = JSONObject.toJSONString(params);
		System.out.println("请求参数："+rsa);
		String rsaStr=null;
		try {
			rsaStr = RSAUtil.encrypt(rsa,DZ_ORDER_DEV,"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		data.put("Data",rsaStr);
		String dataStr = JSONObject.toJSONString(data);
		HttpEntity<String> entity = new HttpEntity(dataStr, headers);
		try {
			ResponseEntity<String> resultEntity = restTemplate.exchange(PN_SIGN_DEV, HttpMethod.POST,entity,String.class);
			System.out.println("平南签收响应："+resultEntity.toString());
			System.out.println("平南签收响应body："+resultEntity.getBody().toString());
			System.out.println("平南签收响应headers："+resultEntity.getHeaders().toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
